<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="养蜂区域名称" prop="areaName">
        <el-input v-model="queryParams.areaName" placeholder="请输入养蜂区域名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="养蜂区域编码" prop="areaCode">
        <el-input v-model="queryParams.areaCode" placeholder="请输入养蜂区域编码" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="所属省份" prop="province">
        <el-input v-model="queryParams.province" placeholder="请输入所属省份" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="所属城市" prop="city">
        <el-input v-model="queryParams.city" placeholder="请输入所属城市" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="所属区县" prop="county">
        <el-input v-model="queryParams.county" placeholder="请输入所属区县" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="区域状态" prop="areaStatus">
        <el-select v-model="queryParams.areaStatus" placeholder="请选择区域状态" clearable>
          <el-option v-for="dict in bee_area_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="养蜂面积" prop="area">
        <el-input v-model="queryParams.area" placeholder="请输入养蜂面积" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['farmer:beearea:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['farmer:beearea:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['farmer:beearea:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['farmer:beearea:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="beeareaList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="养蜂区域信息" align="center" prop="id" />
      <el-table-column label="养蜂区域名称" align="center" prop="areaName" />
      <el-table-column label="养蜂区域编码" align="center" prop="areaCode" />
      <el-table-column label="所属省份" align="center" prop="province" />
      <el-table-column label="所属城市" align="center" prop="city" />
      <el-table-column label="所属区县" align="center" prop="county" />
      <el-table-column label="养蜂面积" align="center" prop="area" />
      <el-table-column label="区域状态" align="center" prop="areaStatus">
        <template #default="scope">
          <dict-tag :options="bee_area_status" :value="scope.row.areaStatus" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['farmer:beearea:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['farmer:beearea:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改蜂区管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="beeareaRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="养蜂区域名称" prop="areaName">
          <el-input v-model="form.areaName" placeholder="请输入养蜂区域名称" />
        </el-form-item>
        <el-form-item label="养蜂区域编码" prop="areaCode">
          <el-input v-model="form.areaCode" placeholder="请输入养蜂区域编码" />
        </el-form-item>
        <el-form-item label="养蜂区域描述" prop="areaDesc">
          <el-input v-model="form.areaDesc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="所属省份" prop="province">
          <el-input v-model="form.province" placeholder="请输入所属省份" />
        </el-form-item>
        <el-form-item label="所属城市" prop="city">
          <el-input v-model="form.city" placeholder="请输入所属城市" />
        </el-form-item>
        <el-form-item label="所属区县" prop="county">
          <el-input v-model="form.county" placeholder="请输入所属区县" />
        </el-form-item>
        <el-form-item label="区域状态" prop="areaStatus">
          <el-select v-model="form.areaStatus" placeholder="请选择区域状态">
            <el-option v-for="dict in bee_area_status" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="养蜂面积" prop="area">
          <el-input v-model="form.area" placeholder="请输入区域面积" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Beearea">
import { listBeearea, getBeearea, delBeearea, addBeearea, updateBeearea } from "@/api/farmer/beearea"

const { proxy } = getCurrentInstance()
const { bee_area_status } = proxy.useDict('bee_area_status')

const beeareaList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    areaName: null,
    areaCode: null,
    province: null,
    city: null,
    county: null,
    areaStatus: null,
  },
  rules: {
    areaName: [
      { required: true, message: "养蜂区域名称不能为空", trigger: "blur" }
    ],
    areaCode: [
      { required: true, message: "养蜂区域编码不能为空", trigger: "blur" }
    ],
    areaDesc: [
      { required: true, message: "养蜂区域描述不能为空", trigger: "blur" }
    ],
    province: [
      { required: true, message: "所属省份不能为空", trigger: "blur" }
    ],
    city: [
      { required: true, message: "所属城市不能为空", trigger: "blur" }
    ],
    county: [
      { required: true, message: "所属区县不能为空", trigger: "blur" }
    ],
    areaStatus: [
      { required: true, message: "区域状态不能为空", trigger: "change" }
    ],
    area: [
      { required: true, message: "养蜂面积不能为空", trigger: "blur" }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询蜂区管理列表 */
function getList() {
  loading.value = true
  listBeearea(queryParams.value).then(response => {
    beeareaList.value = response.rows
    total.value = response.total
    loading.value = false
  })

}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    areaName: null,
    areaCode: null,
    areaDesc: null,
    province: null,
    city: null,
    county: null,
    areaStatus: null,
    createTime: null,
    updateTime: null,
    area: null,
  }
  proxy.resetForm("beeareaRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加蜂区管理"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getBeearea(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改蜂区管理"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["beeareaRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateBeearea(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addBeearea(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除蜂区管理编号为"' + _ids + '"的数据项？').then(function () {
    return delBeearea(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => { })
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('farmer/beearea/export', {
    ...queryParams.value
  }, `beearea_${new Date().getTime()}.xlsx`)
}

getList()
</script>
